import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    menus: JSON.parse(localStorage.getItem("tree")) || [], // 菜单列表
    userInfo: [],
    // userName: JSON.parse(localStorage.getItem("userName")) || "HELLO WORLD"
  },
  mutations: {
    tree: (state, data1) => {
      state.menus = data1;
      localStorage.setItem("tree", JSON.stringify(data1));
    },
    userInfo: (state, data2) => {
      state.userInfo = data2;
      localStorage.setItem("user", JSON.stringify(data2));
    },
    userName: (state, userName) => {
      state.userName = userName;
      // localStorage.setItem("userName", JSON.stringify(userName));
    }
  },
  getters: {
    menuRoutes(state) {
      let dynRoutes = [];
      console.log(state, '0000000000')
      // let meunsLists = JSON.parse(localStorage.getItem("tree")) || [];
      // if (meunsLists == null) return
      state.menus.forEach(v => {
        if (v.children) {
          v.children.forEach(item => {
            let obj = {
              path: item.url,
              name: item.name,
              meta: {
                title: item.displayName
              },
              component: () => import(`@/views/${v.name}/${item.name}.vue`)
            };
            dynRoutes.push(obj);
          });
        }
      });
      // console.log(dynRoutes);
      return dynRoutes;
    }
  },
  actions: {},
  modules: {}
});
// 4. 导出store实例
export default store;